package customer.zspt.cn.customer.components.mine;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.LoginResultDao;
import customer.zspt.cn.customer.components.gbook.gbook;
import customer.zspt.cn.customer.components.order.order;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.activitys.login;
import customer.zspt.cn.customer.sqlites.dao.chatMsgSqliteDao;
import customer.zspt.cn.customer.utils.ChatRecordSPUtils;
import customer.zspt.cn.customer.utils.IntentUtils;
import customer.zspt.cn.customer.utils.SPUtils;

public class mine extends Fragment implements View.OnClickListener {
    private View view;
    @ViewInject(R.id.order_LL)
    private LinearLayout order_LL;
    @ViewInject(R.id.bbs_ll)
    private LinearLayout bbs_ll;
    @ViewInject(R.id.cancal_bt)
    private Button cancal_bt;
    private int userId;
    //数据库操作对象
    private chatMsgSqliteDao msgSqliteDao;
    private Handler mUloginHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            clearAllData();
        }
    };
    @ViewInject(R.id.head_portrait)
    private ImageView head_portrait;
    @ViewInject(R.id.kefuRealname)
    private TextView kefuRealname;
    @ViewInject(R.id.kefuNickname)
    private TextView kefuNickname;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mine, null);
        ViewUtils.inject(this, view);
        initUI();
        getMyInfo();
        return view;
    }

    public void getMyInfo() {
        String info = SPUtils.getString(getContext(), constant.LOGINRESULT, "");
        if (!TextUtils.isEmpty(info)) {
            try {
                toPull(info);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(info);
            }
        }
    }

    private void toPull(String mineInfoData) {
        Gson gson = new Gson();
        LoginResultDao mineInfo = gson.fromJson(mineInfoData, LoginResultDao.class);
        LoginResultDao.LoginResultInfo result = mineInfo.data;
        BitmapUtils bitmapUtils = new BitmapUtils(getContext());
        bitmapUtils.display(head_portrait, result.avatar);
        kefuRealname.setText(result.realname);
        kefuNickname.setText(result.nickname);
        userId = result.userid;
    }

    private void initUI() {
        order_LL.setOnClickListener(this);
        cancal_bt.setOnClickListener(this);
        bbs_ll.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_LL:
                Intent intent = new Intent(getActivity(), order.class);
                intent.putExtra("userid", userId + "");
                startActivity(intent);
                break;
            case R.id.cancal_bt:
                //退出登陆操作
                backKefu();
                break;
            case R.id.bbs_ll:
                IntentUtils
                        .startIntent(getContext(), gbook.class);
                break;
        }
    }

    //清除数据
    private void clearAllData() {
//        PackageManager manager = getContext().getPackageManager();
//        try {
//            PackageInfo packageInfo = manager.getPackageInfo(getContext().getPackageName(), 0);
//            String packageName = packageInfo.packageName;
//            String data = ChatRecordSPUtils.getRecordString(getContext(), constant.HISTROYCHATPEOPLE, constant.HISTROYCHATPEOPLE, "");
//            if (!data.equals("")) {
//                String[] split = data.split(" ");
//                for (int i = 0; i < split.length; i++) {
//                    File file = new File("/data/data/" + packageName + "/shared_prefs", split[i] + ".xml");
//                    if (file.exists()) {
//                        file.delete();
//                    }
//                }
//                for (int i = 0; i < split.length; i++) {
//                    SharedPreferences pref = getContext().getSharedPreferences(split[i], Context.MODE_PRIVATE);
//                    if (pref != null) {
//                        SharedPreferences.Editor editor = pref.edit();
//                        editor.clear();
//                        editor.commit();
//                    }
//                }
//                SPUtils.setString(getContext(), constant.LOGINRESULT, "");
//                SPUtils.setString(getContext(), "token", "");
//                ChatRecordSPUtils.setRecordString(getContext(), constant.HISTROYCHATPEOPLE, constant.HISTROYCHATPEOPLE, "");
//                Toast.makeText(getContext(), "退出成功", Toast.LENGTH_LONG).show();
//                IntentUtils.startIntent(getActivity(), login.class);
//                getActivity().finish();
//            }
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }
        //删除该用户聊天记录数据库
        msgSqliteDao = new chatMsgSqliteDao(getActivity());
        boolean IsDrop = msgSqliteDao.deleteDatabase(getContext(),constant.MSGSQLITENAME);
        if (IsDrop) {
            ChatRecordSPUtils.setRecordString(getContext(), constant.HISTROYCHATPEOPLE, constant.HISTROYCHATPEOPLE, "");
            SPUtils.setString(getContext(), constant.LOGINRESULT, "");
            SPUtils.setString(getContext(), "token", "");
            Toast.makeText(getContext(), "退出成功", Toast.LENGTH_LONG).show();
            IntentUtils.startIntent(getActivity(), login.class);
            getActivity().finish();
        } else {
            Toast.makeText(getContext(), "退出失败", Toast.LENGTH_LONG).show();
        }
    }

    public void backKefu() {
        HttpUtils utils = new HttpUtils();
        String token = SPUtils.getString(getContext(), "token", "");
        RequestParams params = new RequestParams();
        params.addHeader("Authorization", token);
        params.addHeader("Accept", "application/json");
        utils.send(HttpRequest.HttpMethod.POST, constant.ULOGIN, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mUloginHandler.sendEmptyMessage(0);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}
